package com.itcast.stargraph.service.impl;

import com.itcast.stargraph.service.WsNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @Author katana
 * @Date 2025/6/12 15:23
 */
@Service
public class WsNoticeServiceImpl  implements WsNoticeService {
    
    /**
     * IOC中可以用于推送WebSocket消息的消息模板对象
     */
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    
    /**
     * 推送到前端的路径
     */
    private static final String URL = "/topic/messages";
    
    
    /**
     * 推送消息给指定用户
     *
     * @param clientId
     * @param msg
     */
    @Override
    public void sendToUser(String clientId, String msg) {
        //此方法会自动在URL前添加上：/user/clientId的值
        messagingTemplate.convertAndSendToUser(clientId,URL,msg);
    }
    
    /**
     * 广播消息给全部用户
     *
     * @param msg
     */
    @Override
    public void sendToAll(String msg) {
        messagingTemplate.convertAndSend(URL,msg);
    }
}
